//
//  CXDiscoverViewController.m
//  喜马拉雅FM
//
//  Created by hcios on 2017/8/28.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "CXDiscoverViewController.h"
#import "CXDiscoverViewModel.h"
#import "CXDiscoverTableViewCell.h"
#import "CXRefreshStateHeader.h"

@interface CXDiscoverViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) CXDiscoverViewModel * model;

@end

@implementation CXDiscoverViewController

-(void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.navigationItem.title = @"发现";
    
    [self setupNavBar];
    
    [self.tableView.mj_header beginRefreshing];
}


#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 5;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.model.cellCount;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.model cellCount:section];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CXDiscoverTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DiscoverCell" forIndexPath:indexPath];
    
    [cell.iconImageView sd_setImageWithURL:[self.model iconURLWithIndex:indexPath.row Section:indexPath.section] placeholderImage:kGetImage(@"")];
    cell.titleLabel.text = [self.model titleWithIndex:indexPath.row Section:indexPath.section];
    cell.subTitleLabel.text = [self.model subTitleWithIndex:indexPath.row Section:indexPath.section];
    return cell;
}

-(void)setupNavBar {
    
    //消息
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:kGetImage(@"top_message_n") selectedImage:kGetImage(@"top_message_n") target:self action:@selector(didClickMessageBarButton)];
    
    //查询
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:kGetImage(@"top_search") selectedImage:kGetImage(@" ") target:self action:@selector(didClickSearchBarButton)];
}



//点击消息
-(void)didClickMessageBarButton{ }

//点击查询
-(void)didClickSearchBarButton{ }



#pragma mark - lazy load action
-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:@"CXDiscoverTableViewCell" bundle:nil] forCellReuseIdentifier:@"DiscoverCell"];
        // 去掉分割线
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _tableView.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:241 / 255.0 alpha:1];
        _tableView.rowHeight = 55;
        // 上拉刷新
        _tableView.mj_header = [CXRefreshStateHeader headerWithRefreshingBlock:^{
            [self.model getDataCompletionHandle:^(NSError *error) {
                [self.tableView.mj_header endRefreshing];
                [self.tableView reloadData];
            }];
        }];
    }
    return _tableView;
}

-(CXDiscoverViewModel *)model{
    if (!_model) {
        _model = [[CXDiscoverViewModel alloc] init];
    }
    return _model;
}



@end
